#!/bin/sh

# X1Plus boot script
#
# Copyright (c) 2023 - 2024 Joshua Wise, and the X1Plus authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

io -4 -w 0xfe010050 0x11001100 # turn the UART back on

echo "Hi!  I'll prepare the cannon for you!"
echo

. /mnt/sdcard/x1plus/boot.conf
CMDLINE="$CMDLINE x1plus_boot=true"
KDIR=/mnt/sdcard/x1plus/kernel/$KERNEL

# Try to find a device tree that matches.  model isn't good enough, because
# panel_a.dts and panel_c.dts are the same model but a different init sequence.
DTFILES="/proc/device-tree/model"
for i in /proc/device-tree/dsi@ffb30000/panel@*; do
	if [ ! -f $i/status ] ; then
		status="okay"
	else
		status="$(cat $i/status)"
	fi
	if [ "$status" == "okay" ]; then
		DTFILES="$DTFILES $i/panel-init-sequence"
		break
	fi
done
DTHASH=$(cat $DTFILES | md5sum | cut -d' ' -f1)
DTSFILE=$KDIR/$DTHASH.dts

if [ ! -f $DTSFILE ]; then
	echo "Unable to find a device tree that matches the booted printer device tree."
	exit 1
fi
echo "Found a match for this printer's device tree ($DTHASH.dts)."

for i in `cat /proc/cmdline`; do
	# grab a rootfs
	case "$i" in
	androidboot.slot_suffix=*)
		CMDLINE="$CMDLINE $i";;
	esac
done

# Get device SN
if [ -f /oem/device/sn ]; then
    BBL_SN=$(cat /oem/device/sn)
else
    BBL_SN=$(bbl_3dpsn 2> /dev/null)
fi

X1PLUS_DEBUG="false"
if [ "$1" = "debug" ]; then
	X1PLUS_DEBUG="true"
fi

# pass overlay parameters to initramfs
CMDLINE="$CMDLINE bbl_serial=$BBL_SN x1plus_debug=$X1PLUS_DEBUG -- $BASE $CFW $VOLATILE"

# Build our dts
cat "$DTSFILE" | /opt/kexec/dtc -i $KDIR -O dts -o /tmp/boot.dts 2>/dev/null

# Patch and build our dtb, and clean up dts
(sed -e "s#XXXBOOTARGSXXX#$CMDLINE#" -e "s/XXXSERIALNUMBERXXX/$(cat /proc/device-tree/serial-number)/" /tmp/boot.dts) | /opt/kexec/dtc -i $KDIR -O dtb -o /tmp/boot.dtb 2>/dev/null 
rm /tmp/boot.dts

KALLSYMS=$(grep 'T kallsyms_lookup_name' /proc/kallsyms | cut -d' ' -f1)

if grep kexec /proc/cmdline >/dev/null; then
	echo "For some reason, you cannot kexec from a printer that has already kexec'ed once."
	echo "Reboot and try again. (Soft-reboot is OK, no need to hit the power switch.)"
	exit 1
fi

set -x
insmod /opt/kexec/kexec_mod_arm.ko ptr_kallsyms_lookup_name=0x$KALLSYMS
insmod /opt/kexec/kexec_mod.ko ptr_kallsyms_lookup_name=0x$KALLSYMS
/opt/kexec/kexec -l --command-line="$CMDLINE" --dtb=/tmp/boot.dtb --ramdisk=$KDIR/initramfs $KDIR/kernel
set +x

echo
echo 'Ready for blastoff!  Come on, hop into the cannon!'
echo

set -x
mount -o remount,ro /config
mount -o remount,ro /userdata
mount -o remount,ro /mnt/sdcard

/opt/kexec/kexec -f -e
